<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    append-to-body
    :close-on-click-modal="false"
    @close="closeDialog()"
    :visible="visible"
    width="600px"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="100px"
    >
      <el-form-item label="策略名称" prop="policyName">
        <el-input v-model="dataForm.policyName" placeholder="请输入策略名称"></el-input>
      </el-form-item>
      <el-form-item label="策略类型" prop="policyType">
        <el-radio-group v-model="dataForm.policyType">
          <el-radio label="1" border>固定积分</el-radio>
          <el-radio label="2" border>按用户支付价格</el-radio>
          <el-radio label="3" border>按商品销售价格</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item label="固定积分" prop="pointRegular" v-if="dataForm.policyType == '1'">
        <el-input v-model="dataForm.pointRegular" placeholder="请输入固定积分"></el-input>
      </el-form-item>

      <el-form-item label="积分百分比" prop="pointRatio" v-if="dataForm.policyType != '1'">
        <el-input v-model="dataForm.pointRatio" placeholder="请输入积分百分比">
          <template slot="append">%</template>
        </el-input>
      </el-form-item>
      
      <el-form-item label="达标金额" prop="orderPrice">
        <el-input v-model="dataForm.orderPrice" placeholder="请输入达标金额"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="closeDialog">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" v-if="canSubmit"
        >确定</el-button
      >
    </span>
  </el-dialog>
</template>

<script>
import { getObj, addObj, putObj } from "@/api/shop_operate/point_policy";

export default {
  props:{
    visible: {
      type: Boolean,
      default: false
    },
  },
  data() {
    return {
      canSubmit: false,
      dataForm: {
        policyType:'1'
      },
      dataRule: {
        policyName: [
          { required: true, message: "策略名称不能为空", trigger: "blur" },
        ],
        policyType: [
          { required: true, message: "策略类型不能为空", trigger: "blur" },
        ],
        pointRegular: [
          { required: true, message: "固定积分不能为空", trigger: "blur" },
        ],
        pointRatio: [
          { required: true, message: "积分百分比不能为空", trigger: "blur" },
        ],
        orderPrice: [
          { required: true, message: "达标金额不能为空", trigger: "blur" },
        ]
      },

    };
  },
  components: {
    
  },
  methods: {
    init(id) {
      this.canSubmit = true;
      if(id){
        getObj(id).then((response) => {
          this.dataForm = response.data.data
          // 销售价格转元
          this.dataForm.orderPrice = this.formatMoneyToYuan(this.dataForm.orderPrice)
        });
      }
      
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.canSubmit = false;
          let submitData = JSON.parse(JSON.stringify(this.dataForm))
          // 达标金额转分
          submitData.orderPrice = this.formatMoneyToFen(submitData.orderPrice)
          if (submitData.id) {
            putObj(submitData)
              .then((data) => {
                this.$notify.success("修改成功");
                this.$emit("refreshDataList");
                this.closeDialog()
              })
              .catch(() => {
                this.canSubmit = true;
              });
          } else {
            addObj(submitData)
              .then((data) => {
                this.$notify.success("添加成功");
                this.$emit("refreshDataList");
                this.closeDialog()
              })
              .catch(() => {
                this.canSubmit = true;
              });
          }
        }
      });
    },
    //重置表单
    closeDialog() {
      this.$emit('update:visible', false)
    },
  },
};
</script>
